class Solution {
public:
    string addBinary(string a, string b) {
        string s;
        int next = 0;   // 求位的和
        int ai = a.size() - 1, bi = b.size() - 1;
        while(ai >= 0 || bi >= 0)
        {
            if(ai >= 0)
            {
                next += a[ai] - '0';
                ai--;
            }

            if(bi >= 0)
            {
                next += b[bi] - '0';
                bi--;
            }
            s.push_back(next % 2 + '0');
            next /= 2;
        }
        if(next > 0)    // next 还有数
            s.push_back(next % 2 + '0');

        reverse(s.begin(), s.end());

        return s;
    }
};